package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.LitigantDeleteAdapter;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.DisputeDetailBean;
import com.sx.zongzhi.bean.EducationBean;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PartiesBean;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.Global;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新增当时人
 */
public class AddLitigantActivity extends BaseActivity implements IPublicBeanView{

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.et_Name)
    EditText et_Name;
    @BindView(R.id.sp_Sex)
    Spinner sp_Sex;
    @BindView(R.id.sp_Nation)
    Spinner sp_Nation;
    @BindView(R.id.tv_Education)
    TextView tv_Education;
    @BindView(R.id.sp_CredentialsType)
    Spinner sp_CredentialsType;
    @BindView(R.id.et_CredentialsNum)
    EditText et_CredentialsNum;
    @BindView(R.id.et_Contact)
    EditText et_Contact;
    @BindView(R.id.sp_PersonType)
    Spinner sp_PersonType;
    @BindView(R.id.et_Address)
    EditText et_Address;
    /**公共字段获取 性别 民族 证件类型 人员类别*/
    private PublicListPest publicListPest;
    //性别\民族\证件类型\人员类别
    private List<GlobalBean> sexList,nationList,credentialsTypeList,personTypeList;

    //当前当事人学历
    private EducationBean.SchoolingsBean schoolingsBean;
    private List<PartiesBean> partiesBeanList=new ArrayList<>();
    private String currentActivity;
    private DisputeDetailBean disputeDetailBean;
    private LitigantDeleteAdapter deleteAdapter;


    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_add_litigant);
        setTitleName(R.string.litigant);
        setTitleRight("完成");
        publicListPest=new PublicListPest(this);
        ButterKnife.bind(this);
        currentActivity=getIntent().getStringExtra(Global.ACTIVITY_FRAGMENT);
        disputeDetailBean= (DisputeDetailBean) getIntent().getSerializableExtra("DisputeDetailBean");
        initViews();
        initData();
    }

    private void initViews() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
        deleteAdapter=new LitigantDeleteAdapter(this,R.layout.item_text_delete,partiesBeanList);
        recyclerView.setAdapter(deleteAdapter);
        deleteAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                showViews(partiesBeanList.get(position));
            }
        });

        if (disputeDetailBean!=null&&disputeDetailBean.getParties()!=null&&disputeDetailBean.getParties().size()>0){
            partiesBeanList.addAll(disputeDetailBean.getParties());
            showViews(disputeDetailBean.getParties().get(0));
        }
    }

    /**
     *显示数据
     * @param partiesBean
     */
    private void showViews(PartiesBean partiesBean){
        if (partiesBean!=null){
            et_Name.setText(partiesBean.getPartyName());
            et_CredentialsNum.setText(partiesBean.getCertifivateNumber());
            et_Contact.setText(partiesBean.getPartyLinkPattern());
            et_Address.setText(partiesBean.getPartyAddress());
            tv_Education.setText(partiesBean.getSchoolingStr());
            if (schoolingsBean==null){
                schoolingsBean=new EducationBean.SchoolingsBean();
            }
            schoolingsBean.setName(partiesBean.getSchoolingStr());
            schoolingsBean.setValue(partiesBean.getSchooling());
            showSex(partiesBean);
            showNation(partiesBean);
            showPersonType(partiesBean);
            showType(partiesBean);
        }
    }

    private void initData() {
        publicListPest.getPublicList("性别");
        publicListPest.getPublicList("民族");
        publicListPest.getPublicList("证件类型");
        publicListPest.getPublicList("人员类别");
    }


    @OnClick({R.id.tv_right,R.id.tv_Education, R.id.btn_Delete,R.id.btn_Next})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                if (!BaseUtil.isEmpty(et_Name.getText().toString().trim())){
                   if (!submit()){
                       return;
                   }
                }
                MyRxBus.get().post(new MsgEvent<>(currentActivity,1,partiesBeanList));
                finish();
                break;
            case R.id.tv_Education://学历
                startActivityForResult(new Intent(AddLitigantActivity.this,EducationActivity.class),4447);
                break;
            case R.id.btn_Delete://删除
               if (recyclerView.getVisibility()==View.VISIBLE){
                   recyclerView.setVisibility(View.GONE);
               }else {
                   recyclerView.setVisibility(View.VISIBLE);
               }
                break;
            case R.id.btn_Next://新增
                submit();
                break;
        }
    }

    private boolean submit() {
        String name=et_Name.getText().toString().trim();
        if (BaseUtil.isEmpty(name)){
            ToastUtils.showToast(this,"请输入当事人姓名");
            return false;
        }
        GlobalBean sex=(GlobalBean)sp_Sex.getSelectedItem();
        if (sex==null){
            ToastUtils.showToast(this,"请选择性别");
            return false;
        }
        GlobalBean nation=(GlobalBean)sp_Nation.getSelectedItem();
        if (nation==null){
            ToastUtils.showToast(this,"请选择民族");
            return false;
        }
        if (schoolingsBean==null){
            ToastUtils.showToast(this,"请选择当事人学历");
            return false;
        }
        GlobalBean credentialsType=(GlobalBean)sp_CredentialsType.getSelectedItem();
        GlobalBean personType=(GlobalBean)sp_PersonType.getSelectedItem();
        PartiesBean partiesBean =new PartiesBean();
        partiesBean.setPartyName(name);
        partiesBean.setGender(sex);
        partiesBean.setNation(nation);
        partiesBean.setSchooling(schoolingsBean.getValue());
        partiesBean.setSchoolingStr(schoolingsBean.getName());
        partiesBean.setCertifivateType(credentialsType);
        partiesBean.setCertifivateNumber(et_CredentialsNum.getText().toString().trim());
        partiesBean.setPartyLinkPattern(et_Contact.getText().toString().trim());
        partiesBean.setPersonnelCategory(personType);
        partiesBean.setPartyAddress(et_Address.getText().toString().trim());
        if (partiesBeanList==null){
            partiesBeanList=new ArrayList<>();
        }
        partiesBeanList.add(partiesBean);

        et_Name.setText("");
        schoolingsBean=null;
        tv_Education.setText("");
        et_CredentialsNum.setText("");
        et_Contact.setText("");
        et_Address.setText("");
        return true;
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 4447://学历
                    schoolingsBean= ( EducationBean.SchoolingsBean)
                            data.getSerializableExtra("SchoolingsBean");
                    tv_Education.setText(schoolingsBean.getName());
                    break;
            }
        }
    }


    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("性别".equals(publicBean.getDomainName())){
            sexList=publicBean.getDicts();
            if (sexList!=null) {
                SpinnerPubAdapter adapter = new SpinnerPubAdapter(this, R.layout.item_text, sexList);
                sp_Sex.setAdapter(adapter);
            }
        }
        if ("民族".equals(publicBean.getDomainName())){
            nationList=publicBean.getDicts();
            if (nationList!=null) {
                SpinnerPubAdapter adapter = new SpinnerPubAdapter(this, R.layout.item_text, nationList);
                sp_Nation.setAdapter(adapter);
            }
        }
        if ("证件类型".equals(publicBean.getDomainName())){
            credentialsTypeList=publicBean.getDicts();
            if (credentialsTypeList!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,credentialsTypeList);
                sp_CredentialsType.setAdapter(adapter);
            }
        }
        if ("人员类别".equals(publicBean.getDomainName())){
            personTypeList=publicBean.getDicts();
            if (personTypeList!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,personTypeList);
                sp_PersonType.setAdapter(adapter);
            }
        }
        if (disputeDetailBean!=null&&disputeDetailBean.getParties()!=null&&disputeDetailBean.getParties().size()>0){
            showSex(disputeDetailBean.getParties().get(0));
            showNation(disputeDetailBean.getParties().get(0));
            showType(disputeDetailBean.getParties().get(0));
            showPersonType(disputeDetailBean.getParties().get(0));
        }

    }

    private void showPersonType(PartiesBean partiesBean) {
        if (partiesBean.getPersonnelCategory()!=null&&personTypeList!=null) {
            for (int i = 0; i < personTypeList.size(); i++) {
                if (personTypeList.get(i).getId() != partiesBean.getPersonnelCategory().getId()) {
                    sp_PersonType.setSelection(i);
                    break;
                }
            }
        }
    }

    private void showType(PartiesBean partiesBean) {
        if (partiesBean.getCertifivateType() != null&&credentialsTypeList!=null){
            for (int i = 0; i < credentialsTypeList.size(); i++) {
                if (credentialsTypeList.get(i).getId() != partiesBean.getCertifivateType().getId()) {
                    sp_CredentialsType.setSelection(i);
                    break;
                }
            }
        }
    }

    private void showNation(PartiesBean partiesBean) {
        if (partiesBean.getNation()!=null&&nationList!=null) {
            for (int i = 0; i < nationList.size(); i++) {
                if (nationList.get(i).getId() != partiesBean.getNation().getId()) {
                    sp_Nation.setSelection(i);
                    break;
                }
            }
        }
    }

    private void showSex(PartiesBean partiesBean) {
        if (partiesBean.getGender()!=null&&sexList!=null) {
            for (int i = 0; i < sexList.size(); i++) {
                if (sexList.get(i).getId() != partiesBean.getGender().getId()) {
                    sp_Sex.setSelection(i);
                    break;
                }
            }
        }
    }
}
